﻿using System;

namespace SanlieHash.Fast
{
    using SanlieHash.GuoMi;
    public class FastGuoMi
    {
        internal static SM3 sm3 = new SM3();
        internal static SM4 sm4 = new SM4();

        public static string GetSM3(string input)
        {
            return sm3.Make(System.Text.Encoding.UTF8.GetBytes(input));
        }
        public static string EncryptSM4(string input, string key)
        {
            sm4.New();
            sm4.SetKey(FastHash.GetMD5Bytes(key));
            return FastEncode.Base64Encode(sm4.Encrypt(System.Text.Encoding.UTF8.GetBytes(input)));
        }
        public static string DecryptSM4(string input, string key)
        {
            sm4.New();
            sm4.SetKey(FastHash.GetMD5Bytes(key));
            return System.Text.Encoding.UTF8.GetString(sm4.Decrypt(FastEncode.Base64Decode2Bytes(input)));
        }
        public static string EncryptSM4_simple(string input, string key)
        {
            var _key = new byte[16];
            var len = key.Length;
            if (len > 16) len = 16;
            for (int i = 0; i < len; i++) _key[i] = (byte)key[i];
            sm4.New();
            sm4.SetKey(_key);
            return FastEncode.Base64Encode(sm4.Encrypt(System.Text.Encoding.UTF8.GetBytes(input)));
        }
        public static string DecryptSM4_simple(string input, string key)
        {
            var _key = new byte[16];
            var len = key.Length;
            if (len > 16) len = 16;
            for (int i = 0; i < len; i++) _key[i] = (byte)key[i];
            sm4.New();
            sm4.SetKey(_key);
            return System.Text.Encoding.UTF8.GetString(sm4.Decrypt(FastEncode.Base64Decode2Bytes(input)));
        }
        public static ZUC GetZUC(byte[] iv, byte[] key)
        {
            var zuc = new ZUC();
            zuc.Init(iv, key);
            return zuc;
        }
        public static ZUC GetZUC(byte[] iv, string key)
        {
            return GetZUC(iv, FastHash.GetMD5Bytes(key));
        }
        public static ZUC GetZUC(string iv, string key)
        {
            return GetZUC(FastHash.GetMD5Bytes(key), FastHash.GetMD5Bytes(key));
        }
    }
}
